//
//  dataBaseTool.m
//  webViewLocal
//
//  Created by wjc on 2021/12/2.
//

#import "dataBaseTool.h"
#import "WJCDefine.h"
#import <sqlite3.h>
@implementation dataBaseTool
static NSString *dataPath;
+(NSString*)returnDataBasePath{
    if(!dataPath)
    {
        NSString *documentSTR=[NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES ) firstObject];
            //1.创建数据库(文件)
            NSString *dbfilepath=[documentSTR  stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.sqlite",[WJCDeployModel getDeployWithDeployKey:WJCDeployAPPName]?[WJCDeployModel getDeployWithDeployKey:WJCDeployAPPName]:@"sqlLite"]];
        dataPath = dbfilepath;
    }
    return dataPath;
}
+(void)setDataBasePathWithPath:(NSString *)path{
    if (path) {
        dataPath = path;
    }else{
        //对数据库sqlLite进行增删查改
    NSString *documentSTR=[NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES ) firstObject];
        //1.创建数据库(文件)
        NSString *dbfilepath=[documentSTR  stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.sqlite",[WJCDeployModel getDeployWithDeployKey:WJCDeployAPPName]?[WJCDeployModel getDeployWithDeployKey:WJCDeployAPPName]:@"sqlLite"]];
        dataPath = dbfilepath;
    }
}
//+(NSArray *)getDataBaseAllTableName{
//
//}
+(void)setDataBaseWithSQLTxt:(setStrBlock)block andFail:(errorDataBaseBlock)error{
    if (block) {
        NSString *setCode=block();
        if(setCode.length>0){
        if(!dataPath) [dataBaseTool setDataBasePathWithPath:nil];
        sqlite3 *db=NULL;
        int ret= sqlite3_open([dataPath cStringUsingEncoding:NSUTF8StringEncoding]/*转成C语言类型*/, &db);
        if (ret!=SQLITE_OK/*0为创建成功*/) {
            WJCLog(@"创建失败:%s",sqlite3_errmsg(db));
        }
        char *errmsg="数据库错误";
        ret= sqlite3_exec(db/*数据库名*/,[setCode cStringUsingEncoding:NSUTF8StringEncoding]/*要执行的sql语句*/, NULL/*返回语句*/, NULL, &errmsg);
        sqlite3_close(db);
        NSString *errStr=[NSString stringWithFormat:@"%s",errmsg];
        if (errStr.length>0&&errmsg!=NULL) {
            if (error) {
                error(errStr);
            }
        }
    }
    }
}
+(void)getDataBaseWithSQLTxt:(setStrBlock)block andSuccess:(void(^)(id))success andFail:(errorDataBaseBlock)error{
    if (block) {
    NSString *setCode=block();
        if(setCode.length>0){
    if(!dataPath) [dataBaseTool setDataBasePathWithPath:nil];
    
        //参数一数据库所在路径//参数二给定数据库类型对象地址
    sqlite3 *db=NULL;
    int ret= sqlite3_open([dataPath cStringUsingEncoding:NSUTF8StringEncoding]/*转成C语言类型*/, &db);
    if (ret!=SQLITE_OK/*0为创建成功*/) {
        WJCLog(@"创建失败:%s",sqlite3_errmsg(db));
        if (error) {
            error([NSString stringWithFormat:@"%s",sqlite3_errmsg(db)]);
        }
    }
    sqlite3_stmt *stmt;
    NSMutableArray *mutearr=[NSMutableArray new];
    ret= sqlite3_prepare(db, [setCode cStringUsingEncoding:NSUTF8StringEncoding], -1/*sql字符串长度*/, &stmt/*查询返回结果*/, NULL);
    if (ret==SQLITE_OK) {
            //查询成功
        
        while (sqlite3_step(stmt)==SQLITE_ROW) {
            NSMutableDictionary *selectResult=[NSMutableDictionary dictionary];

            for (int i=0; i<sqlite3_column_count(stmt); i++) {
                NSString *str=[NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, i)];

                NSString *columnNamestr=[NSString stringWithUTF8String:(char *)sqlite3_column_name(stmt, i)];
                [selectResult setValue:str forKey:columnNamestr];
            }
           
            [mutearr addObject:selectResult];
        }
            //查询完后释放掉查询结果
        sqlite3_finalize(stmt);
    }
    
    sqlite3_close(db);
        if (success) {
            success(mutearr);
        }
    }
    }
}
@end
